package com.situ.hotel.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import com.situ.hotel.entity.Emp;
import com.situ.hotel.util.JDBCUtil;

// Dao纯粹的做增删改查
public class EmpDao {

	public ArrayList<Emp> selectAll(String searchName) {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		ArrayList<Emp> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`code`,`name`,`birthday`,`address`,`phone`,`password`,`dept_code` FROM `emp` WHERE `name` LIKE ? ";
			statement = connection.prepareStatement(sql);
			// "%" + searchName + "%" 豆豆加加
			statement.setString(1, "%" + searchName + "%");
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String code = resultSet.getString("code");
				String name = resultSet.getString("name");
				String address = resultSet.getString("address");
				String phone = resultSet.getString("phone");
				String password = resultSet.getString("password");
				Date birthday = resultSet.getDate("birthday");
				String deptCode = resultSet.getString("dept_code");
				Emp emp = new Emp(id, code, name, birthday, address, phone, password, deptCode);
				list.add(emp);
			}
			
			for (Emp emp : list) {
				System.out.println(emp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return list;
	}

	public int delete(String code) {
		Connection connection = null;
		PreparedStatement statement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM `emp` WHERE `code`=?";
			statement = connection.prepareStatement(sql);
			// 填上？占位符的值
			statement.setString(1, code);
			// 在执行之前打印statement会打印执行的sql语句
			System.out.println(statement);
			count = statement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return count;
	}

	public int add(Emp emp) {
		Connection connection = null;
		PreparedStatement statement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO `emp`(`code`,`name`,`password`,`phone`,`address`,`birthday`,`dept_code`) VALUES(?,?,?,?,?,?,?)";
			statement = connection.prepareStatement(sql);
			statement.setString(1, emp.getCode());
			statement.setString(2, emp.getName());
			statement.setString(3, emp.getPassword());
			statement.setString(4, emp.getPhone());
			statement.setString(5, emp.getAddress());
			// 真正插入数据库时候将java.util.Date转换为java.sql.Date
			statement.setDate(6, new java.sql.Date(emp.getBirthday().getTime()));
			statement.setString(7, emp.getDeptCode());
			System.out.println(statement);
			count = statement.executeUpdate();
			System.out.println("count: " + count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return count;
	}

}
